//
//  HomePageViewController.m
//  TouTiao
//
//  Created by 柴旭 on 16/9/12.
//  Copyright © 2016年 Match. All rights reserved.
//

#import "HomePageViewController.h"
#import "LZHRecommedTableViewController.h"



@interface HomePageViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *scrollV;

@property(nonatomic,strong)UIButton *selectedBtn;
//显示控制器的scrollview
@property(nonatomic,strong)UIScrollView *contentView;

@property(nonatomic,strong)NSMutableArray *btns;

@end
#define NUM 20
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScrennH [UIScreen mainScreen].bounds.size.height
@implementation HomePageViewController

-(NSMutableArray *)btns{

    if (_btns == nil) {
        _btns = [NSMutableArray array];
    }
    
    return _btns;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航栏左边的按钮
    [self setUpNavigationBarItem];
    
    //设置导航栏的背景颜色
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    //添加子控制器到主viewController上
    [self setUpChildrenControllers];
    
    //添加控制器的scrollview
    [self setUpControllerView];
    
    //给导航栏下面添加标题栏
    [self setUpTitleView];
 
    //关闭这个控制器上的自动适应scrollview的功能
    self.automaticallyAdjustsScrollViewInsets =NO;
    
    //设置标题栏右侧的加号按钮
    [self setUpAddChannelButton];
    
    
    
    
    
    
}
-(void)setUpAddChannelButton{

    UIButton *add = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW*0.85, 64, 57, 44)];
    
    add.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
    [add setImage:[UIImage imageNamed:@"addicon_titlebar_topic"] forState:UIControlStateNormal];
    
    [add addTarget:self action:@selector(showMoreChannel) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:add];


}
//显示另外一个控制器  加载更多的频道
-(void)showMoreChannel{

    NSLog(@"加载更多频道");

    
}

//设置导航栏左边的按钮
-(void)setUpNavigationBarItem{

    //自定义barButton
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [left setImage:[UIImage imageNamed:@"all_toutiaologo_new@3x"] forState:UIControlStateNormal];
    
    left.frame = CGRectMake(0, 0, 100, 44);
    [left sizeToFit];
    left.userInteractionEnabled = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:left];

    
}
//给导航栏下面添加标题栏
-(void)setUpTitleView{

    
    //添加标题view
//    UIView *titleV = [[UIView alloc]init];
    
    
    
    _scrollV = [[UIScrollView alloc]init];
    _scrollV.frame = CGRectMake(0, 64, self.view.frame.size.width, 44);
    _scrollV.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.1];
    _scrollV.showsHorizontalScrollIndicator = NO;
    
    
    //将标题View添加到控制器上
    [self.view addSubview:_scrollV];
    
    
    //添加标题按钮
    NSArray *titles = @[@"推荐",@"热点",@"石家庄",@"视频",@"社会",@"娱乐",@"图片",@"科技",@"汽车",@"体育",@"财经",@"军事",@"国际",@"段子",@"趣图",@"美女",@"健康",@"正能量",@"特卖",@"房产"];
    
    CGFloat W = 60;
    CGFloat H = _scrollV.frame.size.height;
    
    for (int i = 0; i<titles.count; i++) {
        
        UIButton *Btn = [[UIButton alloc]init];
        
        CGFloat X = i*W;
        CGFloat Y = 0;
        
//        Btn.backgroundColor = [UIColor blueColor];
        [Btn sizeToFit];
//
        [Btn layoutIfNeeded];
        //给每一个button加上一个tag值
        Btn.tag = i;
        
        [Btn setTitle:titles[i] forState:UIControlStateNormal];
        [Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        Btn.titleLabel.font = [UIFont systemFontOfSize:18];
        
        Btn.frame = CGRectMake(X, Y, W, H);
        
        [self.btns addObject:Btn];
        
        [Btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//        NSLog(@"%@",Btn);
        
        if (i==0) {
            [self click:Btn];
            
            UIViewController *vc = self.childViewControllers[0];
            vc.view.frame = CGRectMake(0, 0, ScreenW, ScrennH);
            [self.contentView addSubview:vc.view];
        }
        
        //将标题button添加到scrollview上
        [_scrollV addSubview:Btn];
    }
    
    
    
    
    _scrollV.pagingEnabled = YES;
    _scrollV.contentSize = CGSizeMake(titles.count*W, 0);
    _scrollV.contentInset = UIEdgeInsetsMake(0, 0, 0, 60);


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)click:(UIButton *)button{

    self.selectedBtn.selected = NO;
    self.selectedBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    button.selected = YES;
    
    [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:20];
    
    self.selectedBtn = button;
    
    self.contentView.delegate = self;
    
    //点击标题按钮显示对应的控制器
    
    NSInteger index = button.tag;
//    NSLog(@"%ld",index);
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    self.contentView.contentOffset = CGPointMake(offsetX, 0);
    
    UIViewController *VC = self.childViewControllers[index];
    
    VC.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.contentView.bounds.size.height);
    
    [self.contentView addSubview:VC.view];
    
    
}
//添加控制器的scrollview
-(void)setUpControllerView{
    
    self.contentView.userInteractionEnabled = YES;
    
    self.contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 108, ScreenW, ScrennH - 108)];
    //设置滚动视图的滚动范围
    self.contentView.contentSize = CGSizeMake(NUM*ScreenW, 0);
    //设置滚动视图的翻页功能为开启状态
    self.contentView.pagingEnabled = YES;
    
    self.contentView.bounces = NO;
    
    
    [self.view addSubview:self.contentView];
    
}
//当scrollview滚动的时候 的监听方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
 
    
    NSInteger num = scrollView.contentOffset.x / scrollView.bounds.size.width;
    NSLog(@"%ld",(long)num);
    UIViewController *vc = self.childViewControllers[num];
    
    //控制 控制器的尺寸为    滚动了几个控制器就用个数乘以屏幕的宽 宽高位contentView的宽高
    vc.view.frame = CGRectMake(num*ScreenW, 0, ScreenW, self.contentView.bounds.size.height);
    
    vc.view.autoresizesSubviews = YES;
    [self.contentView addSubview:vc.view];
    
    UIButton *followBtn = self.btns[num];
    
    [self click:followBtn];
    [self centerButton:followBtn];
    

    
}
//设置标题按钮文字的居中效果
-(void)centerButton:(UIButton *)button{

    CGFloat offsetX = button.center.x - ScreenW*0.5;
    if (offsetX<0) {
        offsetX = 0;
    }
    CGFloat maxoffsetX = self.scrollV.contentSize.width - ScreenW;
    if (offsetX > maxoffsetX) {
        offsetX = maxoffsetX;
    }
    [self.scrollV setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
    
}
//给内容的scrollview上添加子控制器
-(void)setUpChildrenControllers{


    LZHRecommedTableViewController *tb1 = [[LZHRecommedTableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb1.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    
    [self addChildViewController:tb1];
    
    UITableViewController *tb2 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb2.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    
    [self addChildViewController:tb2];
    
    
    UITableViewController *tb3 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    
    tb3.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb3];
    
    UITableViewController *tb4 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb4.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    
    [self addChildViewController:tb4];
    
    UITableViewController *tb5 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb5.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb5];
    
    
    UITableViewController *tb6 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb6.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb6];
    
    UITableViewController *tb7 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb7.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb7];
    
    UITableViewController *tb8 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb8.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb8];
    
    UITableViewController *tb9 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb9.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb9];
    
    UITableViewController *tb10 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb10.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb10];
    
    UITableViewController *tb11 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb11.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb11];
    
    UITableViewController *tb12 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb12.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb12];
    
    UITableViewController *tb13 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb13.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb13];
    
    UITableViewController *tb14 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb14.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb14];
    
    UITableViewController *tb15 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb15.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb15];
    
    UITableViewController *tb16 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb16.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb16];
    
    UITableViewController *tb17 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb17.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb17];
    
    UITableViewController *tb18 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb18.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb18];
    
    UITableViewController *tb19 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb19.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb19];
    
    UITableViewController *tb20 = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    tb20.view.backgroundColor = LZHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
    [self addChildViewController:tb20];
    

}


-(UIStatusBarStyle)preferredStatusBarStyle{

    return UIStatusBarStyleLightContent;
}
@end
